//
// Created by LONG on 2020/10/18.
//

#include "ueFsm.h"
#include "../../../../../ccore/misc/include/misc.h"

void RAN_L3_UE_SMH_Default(uint32_t state, S_FsmEventContext *evtCtxt, S_FsmHandleResult *result)
{

}

FSM_DEF_BEG(RAN_L3_UE_FSM, RAN_L3_UE_SM_STATE_IDLE)
    FSM_STATE_DEF_BEG(RAN_L3_UE_SM_STATE_IDLE)
        FSM_EVENT_HANDLE_DEF(RAN_L3_UE_SM_EVENT_IDLE, RAN_L3_UE_SMH_Default)
            FSM_STATE_TRANS_DEF(O5G_RC_OK, RAN_L3_UE_SM_STATE_IDLE)
    FSM_STATE_DEF_END(RAN_L3_UE_SM_STATE_IDLE)
FSM_DEF_END(RAN_L3_UE_FSM)

int32_t RAN_L3_UE_SM_Init(void)
{
    uint32_t nbLoop;
    uint32_t cellLoop;
    uint32_t ueLoop;
    S_RanL3NbInst *pNbInst;
    S_RanL3CellInst *pCellInst;
    S_RanL3UeInst *pUeInst;
    char fsmInstId[64];

    gpRanL3Context->ueFsm = FSM_INIT(RAN_L3_UE_FSM);
    IF_RETURN(==, NULL, gpRanL3Context->ueFsm, O5G_RC_ER, "RAN_L3_UE_SM_Init() FSM_INIT(RAN_L3_UE_FSM) error!\n");

    FSM_SetLogCallback(gpRanL3Context->ueFsm, O5G_FSM_Print);

    for (nbLoop = 0; nbLoop < RAN_NODEB_NUM_MAX; nbLoop++)
    {
        pNbInst = &gpRanL3Context->nbInst[nbLoop];
        for (cellLoop = 0; cellLoop < RAN_CELL_NUM_MAX; cellLoop++)
        {
            pCellInst = &pNbInst->cellInst[cellLoop];
            for (ueLoop = 0; ueLoop < RAN_UE_NUM_MAX; ueLoop++)
            {
                pUeInst = &pCellInst->ueInst[ueLoop];
                snprintf(fsmInstId, sizeof(fsmInstId), "nb:%u,cell:%u,ue:%04u", pUeInst->nbIdx, pUeInst->cellIdx, pUeInst->ueIdx);
                pUeInst->ueSmInst = FSM_INST_Init(gpRanL3Context->ueFsm, fsmInstId);
                IF_RETURN(==, NULL, pUeInst->ueSmInst, O5G_RC_ER, "RAN_L3_UE_SM_Init() FSM_INST_Init() error!\n");
            }
        }
    }

    gpRanL3Context->ueFsmEvtQue = FixRB_Create(NULL, 256, sizeof(S_FsmEventContext), FIX_RB_TYPE_NONE);
    IF_RETURN(==, NULL, gpRanL3Context->ueFsmEvtQue, O5G_RC_ER, "RAN_L3_UE_SM_Init() FixRB_Create() error!\n");

    return O5G_RC_OK;
}

int32_t RAN_L3_UE_SM_Final(void)
{
    uint32_t nbLoop;
    uint32_t cellLoop;
    uint32_t ueLoop;
    S_RanL3NbInst *pNbInst;
    S_RanL3CellInst *pCellInst;
    S_RanL3UeInst *pUeInst;

    for (nbLoop = 0; nbLoop < RAN_NODEB_NUM_MAX; nbLoop++)
    {
        pNbInst = &gpRanL3Context->nbInst[nbLoop];
        for (cellLoop = 0; cellLoop < RAN_CELL_NUM_MAX; cellLoop++)
        {
            pCellInst = &pNbInst->cellInst[cellLoop];
            for (ueLoop = 0; ueLoop < RAN_UE_NUM_MAX; ueLoop++)
            {
                pUeInst = &pCellInst->ueInst[ueLoop];
                FSM_INST_Final(pUeInst->ueSmInst);
            }
        }
    }
    FSM_Final(gpRanL3Context->ueFsm);
    FixRB_Destroy(gpRanL3Context->ueFsmEvtQue);

    return O5G_RC_OK;
}

void RAN_L3_UE_SM_IndProcess(S_OsSelectInd *pIndMsg)
{
    switch (pIndMsg->ind)
    {
    case RAN_L3_INTRA_IND_MSG_UE_FSM_EVT:
        break;
    default:
        break;
    }
}

void RAN_L3_UE_SM_MsgProcess(void)
{

}

void RAN_L3_UE_SM_PrdProcess(void)
{

}
